package com.bajiao.app.controller.cart;

import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.web.entity.TableDataInfo;
import com.bajiao.entity.cart.Cart;
import com.bajiao.service.cart.CartService;
import com.bajiao.service.order.OrderService;
import com.bajiao.service.reserve.ReserveService;
import com.bajiao.vo.cart.CartResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 购物车表
 * @Author: FanJq
 * @Date:   2025-01-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags="购物车表")
@RestController
@RequestMapping("/api/v1/cart")
public class RestCartController extends AppBaseController {

	@Resource
	private CartService cartService;
	@Resource
	private OrderService orderService;

	@PostMapping(value = "/create")
	@ApiOperation(value = "添加购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "productId", value = "商品id"),
			@ApiImplicitParam(name = "buyNum", value = "购买数量")
	})
	public RestResult<Cart> create(@ModelAttribute("userId") String userId,
						   @RequestParam("productId") String productId,
						   @RequestParam("buyNum") Integer buyNum) {
		if (StrUtil.isBlank(userId))
			return tokenOverdue();
		if (buyNum <= 0)
			throw new DataHandleException("购买数量不能为0");
		Cart cart = cartService.create(userId, productId, buyNum);
		return success(cart);
	}

	@PostMapping(value = "/delete")
	@ApiOperation(value = "删除购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "购物车id 多个,隔开")
	})
	public RestResult create(@ModelAttribute("userId") String userId,
							 @RequestParam("ids") String ids) {
		if (StrUtil.isBlank(userId))
			return tokenOverdue();
		cartService.delete(userId, ids);
		return success();
	}

	@PostMapping(value = "/modify")
	@ApiOperation(value = "更新购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "productId", value = "商品id"),
			@ApiImplicitParam(name = "buyNum", value = "购买数量")
	})
	public RestResult modify(@ModelAttribute("userId") String userId,
							 @RequestParam("productId") String productId,
							 @RequestParam("buyNum") Integer buyNum) {
		if (StrUtil.isBlank(userId))
			return tokenOverdue();
		if (buyNum <= 0)
			throw new DataHandleException("购买数量不能为0");
		Cart cart = cartService.modify(userId, productId, buyNum);
		return success(cart);
	}

	@GetMapping(value = "/info")
	@ApiOperation(value = "获取购物车信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "storeId", value = "店铺id", dataType = "String", required = true)
	})
	public RestResult<CartResponse> getCartInfo(@ModelAttribute("userId") String userId,
											  @RequestParam(value = "storeId") String storeId) {
		if (StrUtil.isBlank(userId))
			return tokenOverdue();
		Map<String, Object> resultMap = cartService.getCartInfo(userId, storeId);
		return success("", resultMap);
	}

	@GetMapping(value = "/select/amount")
	@ApiOperation(value = "计算选中价格")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "storeId", value = "店铺id", dataType = "String", required = true),
			@ApiImplicitParam(name = "ids", value = "购物车id 多个,隔开", dataType = "String", required = true)
	})
	public RestResult<String> getCartSelectAmount(@ModelAttribute("userId") String userId,
											      @RequestParam(value = "storeId") String storeId,
												  @RequestParam(value = "ids") String ids) {
		if (StrUtil.isBlank(userId))
			return tokenOverdue();
		Map<String, String> resultMap = orderService.getCartAmount(userId, storeId, ids);
		return success(resultMap);
	}
}
